//
//  ImageFilesManager.swift
//  WeChat
//
//  Created by mac on 2018/8/14.
//  Copyright © 2018年 RRC. All rights reserved.
//

import Foundation
import Kingfisher


/// 围绕 Kingfisher 构建的缓存器，先预存图片名称，等待上传完毕后改成 URL 的名字
class ImageFilesManager {
    let imageCacheFolder = KingfisherManager.shared.cache
    
    @discardableResult /*有返回值的方法调用时必须要有一个接收方，否则会有警告，使用 @discardableResult 不处理的时候就不会有警告了，也可以用一个通配符接收方法返回值 */
    class func cachePathForKey(_ key:String) -> String? {
        let fileName = key.ts_MD5String
        return (KingfisherManager.shared.cache.diskCachePath as NSString).appendingPathComponent(fileName)
    }
    
    class func stroeImage(_ image:UIImage, key:String, completionHandler:(() -> ())?) {
        KingfisherManager.shared.cache.removeImage(forKey: key)
        KingfisherManager.shared.cache.store(image, forKey: key, toDisk: true, completionHandler: completionHandler)
    }
    
    
    /// 修改文件名称
    ///
    /// - Parameters:
    ///   - originPath: 原路径
    ///   - destinationPath: 目标路径
    /// - Returns: 目标路径
    @discardableResult
    class func renameFile(_ originPath: URL, destinationPath:URL) -> Bool {
        do {
            try FileManager.default.moveItem(atPath: originPath.path, toPath: destinationPath.path)
            return true
        } catch let error as NSError {
            print(error.debugDescription)
            return false
        }
    }
    
}
